package com.bazaaroid.client.web.gwt.partnermodule.client.view.locations;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.LocationDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.locations.impl.LocationsTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LocationEntryView extends Composite {

	private LocationsTabViewImpl locationsTabView;
	private LocationDTO locationDTO;
	private TitleCommandBar titleBar;
	private HTML point = new HTML();
	
	public class EditClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			locationsTabView.startEdit(LocationEntryView.this);
		}
	}
	
	public class DeleteClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(Window.confirm("Are you sure you wand to delete '" + locationDTO.getName() + "'?")) {
				locationsTabView.getLocationViewHandler().onLocationEntryDeleted(locationDTO);
				removeFromParent();
			}
		}
	}
	
	public LocationEntryView(LocationsTabViewImpl locationsTabView, LocationDTO locationDTO) {
		this.locationsTabView = locationsTabView;
		this.locationDTO = locationDTO;
		
		VerticalPanel locationPanel = new VerticalPanel();
		initWidget(locationPanel);
		
		titleBar = new TitleCommandBar(locationDTO.getName());
		titleBar.addCommand("edit", new EditClickHandler());
		titleBar.addCommand("delete", new DeleteClickHandler());
		locationPanel.add(titleBar);
		locationPanel.add(point);
		locationPanel.setStyleName("locationPanel");
		point.setStyleName("locationDescription");
		//titleBar.addStyleName("");
		update();
	}
	
	public void update() {
		titleBar.setTitle(locationDTO.getName());
		point.setHTML(locationDTO.getLatitude() + ", " + locationDTO.getLongitude());
	}

	public LocationDTO getLocationDTO() {
		return locationDTO;
	}

	public LocationsTabViewImpl getLocationsTabView() {
		return locationsTabView;
	}
}
